package app;

import util.Utils;

public class CommandBuilder {

	public static final short CONNECT_REQ_ID = 1;
	public static final short CONNECT_RSP_ID = 2;
	public static final short DISCONNECT_REQ_ID = 7;
	public static final short DISCONNECT_RSP_ID = 8;
	public static final short PARLIST_REQ_ID = 11;
	public static final short PARLIST_RSP_ID = 12;
	public static final short SINGLETUNE_REQ_ID = 15;
	public static final short SINGLETUNE_RSP_ID = 16;
	public static final short TICKET_DISABLED = 0;
	public static final short DEFAULT_TICKET_INTERVAL = 300; // 300s = 5min ;
	// max 10000s

	public static final short STANDARD_SIZE_OF_CONNECT_REQ_MSG = 10; // bytes
	public static final short STANDARD_SIZE_OF_DISCONNECT_REQ_MSG = 8; // bytes
	public static final short STANDARD_SIZE_OF_PARLIST_REQ_MSG = 8; // bytes
	public static final short STANDARD_SIZE_OF_SINGLETUNE_REQ_MSG = 8; // bytes

	public CommandBuilder() {
		// TODO Auto-generated constructor stub
	}

	public byte[] build_ConnectReq(short _destID, short _srcID, short _ticketInterval) {

		// CONNECT_REQ_MSG = <trans_header> <connect_req> <ticket_interval>
		// <trans_header> = <trans_len> <destID> <srcID> ( 2bytes + 2bytes +
		// 2bytes = 6bytes)

		// <trans_header> + CONNECT_REQ + <ticket_interval> ( 6bytes + 2bytes + 2bytes = 10bytes)
		// <trans_len> = valor total em bytes do comando (valor PAR entre 8..504)

		byte[] commandConnectReq = new byte[CommandBuilder.STANDARD_SIZE_OF_CONNECT_REQ_MSG];
		byte[] length = Utils.shortToBytes(CommandBuilder.STANDARD_SIZE_OF_CONNECT_REQ_MSG);
		
		commandConnectReq[0] = length[0];
		commandConnectReq[1] = length[1];
		
		// <destID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] destID = Utils.shortToBytes(_destID);
		
		// <srcID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] srcID = Utils.shortToBytes(_srcID);
		
		byte[] connect_req_id = Utils.shortToBytes(CONNECT_REQ_ID);
		byte[] ticketInterval = Utils.shortToBytes(_ticketInterval);

		
		commandConnectReq[2] = destID[0];
		commandConnectReq[3] = destID[1];

		commandConnectReq[4] = srcID[0];
		commandConnectReq[5] = srcID[1];

		commandConnectReq[6] = connect_req_id[0];
		commandConnectReq[7] = connect_req_id[1];

		commandConnectReq[8] = ticketInterval[0];
		commandConnectReq[9] = ticketInterval[1];

		// switch to 'Little Endian' format
		// TODO : Test if it works
		Utils.fixEndianess_2by2Bytes(commandConnectReq);
		
		//TODO: append escape sequence to MSG
		//TODO: append synchronization char to MSG
		
		return commandConnectReq;
	}

	
	
	public byte[] build_DisconnectReq(short _destID, short _srcID) {

		// DISCONNECT_REQ_MSG = <trans_header> <disconnect_req> 
		// <trans_header> = <trans_len> <destID> <srcID> ( 2bytes + 2bytes +
		// 2bytes = 6bytes)

		// <trans_header> + DISCONNECT_REQ ( 6bytes + 2bytes = 8 bytes)
		// <trans_len> = valor total em bytes do comando (valor PAR entre 8..504)

		byte[] commandDisconnectReq = new byte[CommandBuilder.STANDARD_SIZE_OF_DISCONNECT_REQ_MSG];
		byte[] length = Utils.shortToBytes(CommandBuilder.STANDARD_SIZE_OF_DISCONNECT_REQ_MSG);
		
		commandDisconnectReq[0] = length[0];
		commandDisconnectReq[1] = length[1];
		
		// <destID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] destID = Utils.shortToBytes(_destID);
		
		// <srcID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] srcID = Utils.shortToBytes(_srcID);
		
		byte[] disconnect_req_id = Utils.shortToBytes(DISCONNECT_REQ_ID);
		
		
		commandDisconnectReq[2] = destID[0];
		commandDisconnectReq[3] = destID[1];

		commandDisconnectReq[4] = srcID[0];
		commandDisconnectReq[5] = srcID[1];

		commandDisconnectReq[6] = disconnect_req_id[0];
		commandDisconnectReq[7] = disconnect_req_id[1];

		
		// switch to 'Little Endian' format
		// TODO : Test if it works
		Utils.fixEndianess_2by2Bytes(commandDisconnectReq);
		
		//TODO: append escape sequence to MSG
		//TODO: append synchronization char to MSG
		
		return commandDisconnectReq;
	}
	
	
	public byte[] build_ParListReq(short _destID, short _srcID) {

		// PARLIST_REQ_MSG = <trans_header> <parlist_req> 
		// <trans_header> = <trans_len> <destID> <srcID> ( 2bytes + 2bytes +
		// 2bytes = 6bytes)

		// <trans_header> + PARLIST_REQ ( 6bytes + 2bytes = 8 bytes)
		// <trans_len> = valor total em bytes do comando (valor PAR entre 8..504)

		byte[] commandParListReq = new byte[CommandBuilder.STANDARD_SIZE_OF_PARLIST_REQ_MSG];
		byte[] length = Utils.shortToBytes(CommandBuilder.STANDARD_SIZE_OF_PARLIST_REQ_MSG);
		
		commandParListReq[0] = length[0];
		commandParListReq[1] = length[1];
		
		// <destID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] destID = Utils.shortToBytes(_destID);
		
		// <srcID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] srcID = Utils.shortToBytes(_srcID);
		
		byte[] parlist_req_id = Utils.shortToBytes(PARLIST_REQ_ID);
		
		
		commandParListReq[2] = destID[0];
		commandParListReq[3] = destID[1];

		commandParListReq[4] = srcID[0];
		commandParListReq[5] = srcID[1];

		commandParListReq[6] = parlist_req_id[0];
		commandParListReq[7] = parlist_req_id[1];

		
		// switch to 'Little Endian' format
		// TODO : Test if it works
		Utils.fixEndianess_2by2Bytes(commandParListReq);
		
		//TODO: append escape sequence to MSG
		//TODO: append synchronization char to MSG
		
		return commandParListReq;
	}
	
	
	
	//incompleto 
	public byte[] build_SingleTuneReq(short _destID, short _srcID) {

		// SINGLETUNE_REQ_MSG = <trans_header> <singletune_req> 
		// <trans_header> = <trans_len> <destID> <srcID> ( 2bytes + 2bytes +
		// 2bytes = 6bytes)

		// <trans_header> + SINGLETUNE_REQ ( 6bytes + 2bytes = 8 bytes)
		// <trans_len> = valor total em bytes do comando (valor PAR entre 8..504)

		byte[] commandSingleTuneReq = new byte[CommandBuilder.STANDARD_SIZE_OF_SINGLETUNE_REQ_MSG];
		byte[] length = Utils.shortToBytes(CommandBuilder.STANDARD_SIZE_OF_SINGLETUNE_REQ_MSG);
		
		commandSingleTuneReq[0] = length[0];
		commandSingleTuneReq[1] = length[1];
		
		// <destID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] destID = Utils.shortToBytes(_destID);
		
		// <srcID> = 16bits >> 0..2^(16-1) ((0..9) && 32865 reservados)
		byte[] srcID = Utils.shortToBytes(_srcID);
		
		byte[] singletune_req_id = Utils.shortToBytes(SINGLETUNE_REQ_ID);
		
		
		commandSingleTuneReq[2] = destID[0];
		commandSingleTuneReq[3] = destID[1];

		commandSingleTuneReq[4] = srcID[0];
		commandSingleTuneReq[5] = srcID[1];

		commandSingleTuneReq[6] = singletune_req_id[0];
		commandSingleTuneReq[7] = singletune_req_id[1];

		
		// switch to 'Little Endian' format
		// TODO : Test if it works
		Utils.fixEndianess_2by2Bytes(commandSingleTuneReq);
		
		//TODO: append escape sequence to MSG
		//TODO: append synchronization char to MSG
		
		return commandSingleTuneReq;
	}
	
	
	
	
	
	
}
